package com.example.student;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Console {

    private static final StudentManage studentManage = new StudentManage();

    public static void index() {
        System.out.println("============================");
        System.out.println("=     学生信息管理系统     =");
        System.out.println("============================");
        System.out.println("=1.班级管理                =");
        System.out.println("=2.学生查询                =");
        System.out.println("============================");

    }

    public static void findClassIfCs() {
        System.out.println("============================");
        System.out.println("=    班 级 管 理 系 统     =");
        System.out.println("============================");
        System.out.println("=1.班级信息查询            =");
        System.out.println("=2.根据学生名称查班级       =");
        System.out.println("=3.根据学号查询班级         =");
        System.out.println("=4.新增班级                =");
        System.out.println("=5.根据班级名称查询班级     =");
        System.out.println("============================");
    }

    public static void findClassIf(Scanner scanner) {
        findClassIfCs();
        while (true) {
            String nextLine = scanner.nextLine();
            if ("-1".equals(nextLine)) {
                System.out.println("返回首页");
                index();
                break;
            }
            switch (nextLine) {
                case "1":
                    System.out.println("班级信息查询");

                    List<SchoolClass> allSchoolClasses = studentManage.getAllSchoolClasses();
                    printSchoolClassInfo(allSchoolClasses);

                    break;
                case "2":
                    System.out.println("根据学生名称查班级");
                    break;
                case "3":
                    System.out.println("根据学号查询班级");
                    break;
                case "4":
                    System.out.println("新增班级");
                    addClassInfo(scanner);
                    break;
                case "5":
                    System.out.println("根据班级名称查询班级");
                    findClassByClassNo(scanner);
                    break;

                default:
                    System.out.println("请重新输入");
                    break;
            }
            findClassIfCs();

        }

    }

    /**
     * 显示班级信息
     * 
     * @param allSchoolClasses 班级信息
     */
    public static void printSchoolClassInfo(List<SchoolClass> allSchoolClasses) {
        if (allSchoolClasses == null) {
            System.out.println("该值为空，请重新输入");
            return;
        }
        System.out.println("====================================");
        for (int i = 0; i < allSchoolClasses.size(); i++) {
            SchoolClass schoolClass = allSchoolClasses.get(i);
            String className = schoolClass.getClassname();
            String classNo = schoolClass.getClassId();
            int studentSize = schoolClass.getStudents().size();
            System.out.println("=班级名称:" + className + "班级编号:" + classNo + "学生数量:" + studentSize + "=");

        }
        System.out.println("====================================");

    }

    /**
     * 新增班级信息
     */

    public static void addClassInfo(Scanner scanner) {
        System.out.println("==========================================");
        System.out.println("=             班级信息                     ");
        System.out.println("==========================================");
        String className = "";
        String classNo = "";

        while (true) {
            System.out.println("输入班级名称");
            className = scanner.nextLine();

            System.out.println("输入班级编号");
            classNo = scanner.nextLine();

            // 检测班级是否存在
            SchoolClass schoolClassByname = studentManage.findSchoolClassByName(className);
            SchoolClass schoolClassByNO = studentManage.findSchoolClassByNo(classNo);
            if (schoolClassByname != null || schoolClassByNO != null) {
                System.out.println("该班级已存在，请重新输入");
            } else {
                break;
            }

        }

        SchoolClass schoolClass = new SchoolClass();
        schoolClass.setClassname(className);
        schoolClass.setClassId(classNo);
        studentManage.addClass(schoolClass);
        System.out.println("新增成功");

    }

    /**
     * 根据班级名称查询班级信息
     * 
     * @param scanner
     */
    public static void findClassByClassNo(Scanner scanner) {
        System.out.println("===========================================");
        System.out.println("=              班级信息                   =");
        System.out.println("===========================================");
        while (true) {
            // 输入班级名称，查询该班级是否存在
            System.out.println("请输入待查询的班级名称(输入-1退出)：");
            String className = scanner.nextLine();
            if ("-1".equals(className)) {
                System.out.println("返回上级菜单");
                break;
            }
            SchoolClass schoolClassByname = studentManage.findSchoolClassByName(className);

            // 该班级存在，显示该班级信息
            if (schoolClassByname != null) {
                List<SchoolClass> schoolClasses = new ArrayList<SchoolClass>();
                schoolClasses.add(schoolClassByname);
                printSchoolClassInfo(schoolClasses);
            } else {
                System.out.println("输入的" + scanner.nextLine() + "班级不存在");
            }
            // 该班级不存在

        }

    }

    public static void findStudentIf(Scanner scanner) {
        System.out.println("查询学生信息");
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in, "GBK");
        index();
        do {
            String nextLine = scanner.nextLine();
            if ("-1".equals(nextLine)) {
                System.out.println("结束运行");
                break;
            }
            switch (nextLine) {
                case "1":
                    findClassIf(scanner);
                    break;
                case "2":
                    findStudentIf(scanner);
                    break;
                default:
                    System.out.println("请重新选择");
                    index();
                    break;
            }
        } while (scanner.hasNext());
        scanner.close();

    }
}
